<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class Ad extends BaseTopic implements iEditEverything {

	private $game_manager;
	private $region_manager;
	private $toolbox;

	public function __construct($page, $id = 0) {
		parent::__construct($page, $id);
		$this->game_manager = new GameManager();
		$this->checker = new InputChecker();
		$this->region_manager = new RegionManager();
		$this->toolbox = new Toolbox();
	}

	public function set_game_manager($game_manager) {
		$this->game_manager = $game_manager;
	}

	public function set_region_manager($region_manager) {
		$this->region_manager = $region_manager;
	}

	public function set_toolbox($toolbox) {
		$this->toolbox = $toolbox;
	}

	public function prepare($new, $item, $inputs) {
		
		array_pop($inputs); // cuts title
		
		$inputs[] = $this->creator->input_field("Titel", "title", $item['title'], 20, 100, "", 1);
		
		//TODO: extract javascript
		$inputs[] = $this->creator->info("De titel wordt automatisch ingevuld.
		<script type=\"text/javaScript\">
			function setTitle() {
				if (document.itemform.seek[0].checked)
					var str = 'GZ';
				else
					var str = 'TK';
				str += ': ' + document.itemform.what.value;
				document.itemform.title.value = str;
			}
		</script>");
		
		$inputs[] = $this->creator->input_radio("", "seek", array("GZ","TK"), array("zoeken", "aanbieden"), "GZ", "setTitle()");
		
		$inputs[] = $this->creator->input_field("Wat", "what", substr($item['title'], 4), 20, 100, "setTitle()");
		$inputs[] = $this->creator->info("bvb: 'spelers', 'regelboek 4e editie', 'dobbelstenen' ...");
		
		$game_types = $this->toolbox->get_keys_and_values("game_type_id", "game_type", "GAME_TYPE");
		$game_kv = $this->game_manager->game_get_games_kv();

		array_unshift($game_kv['keys'], "NEW");
		array_unshift($game_kv['values'], "NIEUW SPEL");
		array_unshift($game_kv['keys'], "NONE");
		array_unshift($game_kv['values'], "GEEN");

		$inputs[] = $this->creator->input_select("Spel", 
				"game_id", 
				$game_kv['keys'], 
				$game_kv['values'], 
				"kies een spel", 
				ereg("^[0-9]+$", $item['game_id']) ? $item['game_id'] : "NONE", 
				"document.itemform.submit()"); //TODO: extract javascript
		if ($_POST["game_id"] == "NEW") {
			$inputs[] = $this->creator->input_select("Spel - soort", "game_type", $game_types['keys'], $game_types['values']);
			$inputs[] = $this->creator->input_field("Spel - naam", "game_name");
		}

		if (($_POST['region'] == "NEW")) {
			$kv = $this->region_manager->region_get_regions_kv(1);
			$inputs[] = $this->creator->input_field("Regio", "region_name");
			$inputs[] = $this->creator->input_select("Locatie", "region", $kv['keys'], $kv['values'], "kies een regio", $item['region_id']);
		} else {
			$kv = $this->region_manager->region_get_regions_kv();
			$inputs[] = $this->creator->input_select("Regio", "region", 
					array_merge(array("NEW"), $kv['keys']), 
					array_merge(array("NIEUWE REGIO"), $kv['values']), 
					"kies een regio", 
					$item['region_id'], 
					"document.itemform.submit()");
		}
		
		$inputs[] = $this->creator->input_text("Beschrijving", "body", 
				$this->db_access->db_desqlize($item['body']));
		return $inputs;
	}

	public function get_edit_everything() {
		$sql = $this->db_access->db_query_select(array(array('*')), array("AD"), "item_id = {$this->id}");
		$sqlresult = $this->db_access->db_do_query($sql);
		$result = $this->db_access->db_fetch_array($sqlresult);
		return $result;
	}

	public function check_content($values) {
		$ok = $this->checker->checkBody($values);
		if ($values["game_id"] != "NONE" && $values["game_id"] == "NEW") {
			$ok = $ok && $this->checker->check_int($values["game_type"], "spel - soort");
			$ok = $ok && $this->checker->check_input($values["game_type"], "spel - soort niet ingevuld");
			$ok = $ok && $this->checker->check_exists($values["game_name"], "spel - naam");
			$ok = $ok && $this->checker->check_tags($values["game_name"], "spel - naam");
		} else if ($values["game_id"] != "NONE") {
			$ok = $ok && $this->checker->check_int($values["game_id"], "spel");
			$ok = $ok && $this->checker->check_input($values["game_id"] > 0, "spel niet ingevuld");
		}
		if (ereg("^parent[0-9]+$", $values['region'])) {
			$ok = $ok && $this->checker->check_exists($values['region_name'], "naam regio");
			return $ok && $this->checker->check_tags($values['region_name'], "naam regio");
		} 
		return $ok && $this->checker->check_input($values['region'] > 0, "regio niet aangegeven");
	}

	protected function derive_info() {
		$info = $this->region_manager->region_format(
					$this->region_manager->region_get_region_tree($_POST['region']));
		if (ereg("^[0-9]+$", $_POST['game_id'])) {
			$sqlresult = $this->db_access->db_do_query(
					$this->db_access->db_query_select(array(array('name')), 
							array('GAME'), "game_id = {$_POST['game_id']}"));
			if ($this->db_access->db_num_rows($sqlresult) == 1) {
				$g = $this->db_access->db_fetch_array($sqlresult);
				return "{$g['name']} - $info";
			}
		} else if (strlen($_POST['game_name']) > 0) {
			return "{$_POST['game_name']} - $info";
		}
		return $info;
	}

	protected function get_sql_from_post($item_id) {
		if (ereg("^parent[0-9]+$", $_POST['region'])) { // new region
			$this->db_access->db_do_query(
					$this->db_access->db_query_insert(
							array('name'), 
							array($this->db_access->db_sqlize($_POST['region_name'])), 
									"REGION"));

			$region_id = $this->db_access->db_insert_id();

			$this->db_access->db_do_query(
					$this->db_access->db_query_insert(
							array('parent_region', 'sub_region'), 
							array(substr($_POST['region'], 6), $region_id), "REGION_TREE"));
		} else {
			$region_id = $_POST['region'];
		}
		
		if ($_POST["game_id"] == "NEW") {
			$this->db_access->db_do_query( 
					$this->db_access->db_query_insert(array('name'), 
							array($this->db_access->db_sqlize($_POST["game_name"])), "GAME")); 
			$game_id = $this->db_access->db_insert_id(); 
			$this->db_access->db_do_query( 
					$this->db_access->db_query_insert(array('game_id', 'game_type_id'),  
							array($game_id, $_POST["game_type"]), "GAME_OF_TYPE"));
		} else if ($_POST['game_id'] == "NONE") {
			$game_id = "NULL";
		} else {
			$game_id = $_POST["game_id"];
		}
			
		return $this->db_access->db_query_insert( 
				array('item_id', 'game_id', 'region_id', 'body', 'seek'), 
				array($item_id, $game_id, $region_id, 
						$this->db_access->db_sqlize($_POST['body']),
						($_POST['seek'] == "GZ") ? 1 : 0), "AD");
	}
}

?>
